package com.qdxht.dispenser.utils;


import com.qdxht.dispenser.ui.models.entity.MLatLng;

public class GPSUtils {

    // 计算两点距离
    private static final double EARTH_RADIUS = 6378137.0;

    public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
        double radLat1 = (lat_a * Math.PI / 180.0);
        double radLat2 = (lat_b * Math.PI / 180.0);
        double a = radLat1 - radLat2;
        double b = (lng_a - lng_b) * Math.PI / 180.0;
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    public static double[] bd09ToGps0(double gg_lat, double gg_lon) {
        double pi = 3.1415926535897932384626;
        double x = gg_lon - 0.0065, y = gg_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
        double gg_lon1 = z * Math.cos(theta);
        double gg_lat1 = z * Math.sin(theta);
        return new double[]{gg_lat1 + 0.006, gg_lon1 + 0.0065};
    }

    public static MLatLng bd09ToGps(double gg_lat, double gg_lon) {
        double pi = 3.1415926535897932384626;
        double x = gg_lon - 0.0065, y = gg_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
        double gg_lon1 = z * Math.cos(theta);
        double gg_lat1 = z * Math.sin(theta);
        return new MLatLng(gg_lat1 + 0.006, gg_lon1 + 0.0065);
    }
}
